<template>
  <div class="home_container">
    <div class="fold" @click="fold($event)" v-btn="$store.state.AsideMenu.fold">
      <i>&lt;</i>
    </div>
    <el-container>
      <el-header>
        <div>
          <img src="../assets/images/heima.png" alt="" />
          <span>百货STORE后台管理系统</span>
        </div>
        <button @click="exit" class="header_btn">退出</button>
      </el-header>
      <el-container>
        <el-aside :width="currentWith">
          <!-- 侧边栏组件 -->
          <MenuAside :list="list" />
        </el-aside>
        <el-main>
          <!-- 主体组件 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import MenuAside from "../components/Menu/MenuAside.vue"
import { mapState } from "vuex"
export default {
  name: "Home",
  data() {
    return {
      list: "",
      toggle: false,
    }
  },
  components: {
    MenuAside,
  },
  created() {
    this.getData()
  },
  methods: {
    exit() {
      sessionStorage.clear()
      location.reload()
    },
    getData() {
      this.$http
        .get("/menus")
        .then((response) => {
          const { data, status } = response
          if (status != 200) {
            this.$message.error("请求失败")
            return
          }
          this.list = data.data
        })
        .catch((error) => {
          console.log(error.message)
        })
    },

    fold() {

      this.toggle = !this.toggle
      this.$store.commit("AsideMenu/toggle", this.toggle)
    },
  },
  computed: {
    ...mapState("AsideMenu", {
      with: "fold",
    }),
    currentWith: {
      get() {
        if (this.with) {
          return "100px"
        } else {
          return "200px"
        }
      },
    },
  },
  directives: {
    btn: {
      update(element, binding) {
        if (binding.value) {
          element.style.left = "90px"
          element.children[0].innerHTML = "&gt;"
        } else {
          element.style.left = "190px"
          element.children[0].innerHTML = "&lt;"
        }
      },
    },
  },
}
</script>

<style scoped>
.home_container {
  height: 100%;
  background-color: #fff;
  color: white;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #313441;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-aside {
  background-color: #333744;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #eaedf1;
  text-align: center;
}
.header_btn {
  display: block;
  background-color: #898e96;
  border: 0;
  height: 64%;
  width: 56px;
  border-radius: 9px;
}
.fold {
  position: absolute;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background-color: #ccc;
  left: 190px;
  top: 216px;
  cursor: pointer;
  z-index: 999;
}
.fold i {
  position: absolute;
  left: 47%;
  top: 48%;
  transform: translate(-50%, -50%);
}
</style>
